package com.zhny.aiot.gateway.plugin.device;

import com.google.common.collect.ImmutableMap;
import com.zhny.aiot.gateway.plugin.model.GatewayStoreEntry;
import com.zhny.aiot.plugin.ICenterFacilities;
import com.zhny.aiot.plugin.IDeviceFactory;
import com.zhny.aiot.plugin.core.utils.MessageUtils;
import com.zhny.aiot.plugin.model.DeviceEntity;
import com.zhny.aiot.plugin.model.MessageEntry;
import com.zhny.aiot.plugin.model.MessageType;
import com.zhny.aiot.plugin.model.TaskEntity;
import com.zhny.aiot.plugin.zhnygateway.GatewayEngine;
import com.zhny.aiot.plugin.zhnygateway.GatewayServer;
import com.zhny.aiot.plugin.zhnygateway.device.ZhnyConfig;
import com.zhny.aiot.plugin.zhnygateway.device.ZhnyDevice;
import com.zhny.aiot.plugin.zhnygateway.model.GatewayResponseMessage;
import com.zhny.aiot.plugin.zhnygateway.model.NodeRequestMessage;
import org.pf4j.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

public class GatewayDevice extends ZhnyDevice<GatewayStoreEntry> {
    private static final Logger logger = LoggerFactory.getLogger(GatewayDevice.class);
    private final Map<String, Integer> CommandOperations;
    public GatewayDevice(GatewayEngine engine, IDeviceFactory<ZhnyConfig> factory, ICenterFacilities centerFacilities, DeviceEntity entity) {
        super(engine, factory, centerFacilities, entity);
        CommandOperations =  new ImmutableMap.Builder<String, Integer>()
                .put("reboot", 0x0f).build();
    }

    @Override
    public void onDecoder(GatewayResponseMessage message) {
        logger.info (message.toString ());
        int type = message.getDeviceType ();
        if(type == 0x0A){
            logger.info("---start decoder gateway message---");
            onMessage(message);
        }
        if(message.getDeviceData() !=null)
            message.getDeviceData ().release ();
    }

    private void onMessage(GatewayResponseMessage message) {
        MessageEntry resultMsg = MessageUtils.createMessageEntry (this.getId ());
        resultMsg.setType(MessageType.RESULT);
        GatewayStoreEntry storeEntry = new GatewayStoreEntry ();
        int operation = message.getDeviceOper ();
        if(operation >= 0x01 && operation <= 0x0f){
            String key = getConfigKey();
            if(operation == 0x01){
                String describe = String.format("协调器%s数据打包成功",key);
                storeEntry.setDescription (describe);
                resultMsg.setData (storeEntry);
            }else if(operation == 0x04){
                String describe = String.format("协调器%s数据接收校验失败",key);
                storeEntry.setDescription (describe);
                resultMsg.setType(MessageType.ERROR);
                resultMsg.setData (storeEntry);
            }else if(operation == 0x05){
                String describe = String.format("协调器%s接收数据包太大",key);
                storeEntry.setDescription (describe);
                resultMsg.setType(MessageType.ERROR);
                resultMsg.setData (storeEntry);
            }else if(operation == 0x06){
                String describe = String.format("协调器%s全部操作成功",key);
                storeEntry.setDescription (describe);
                resultMsg.setData (storeEntry);
            }else if(operation == 0x0b){
                int deviceNo = message.getDeviceData().readUnsignedShort();
                String describe = String.format("控制器%s通信失败",deviceNo);
                storeEntry.setDescription (describe);
                resultMsg.setType(MessageType.ERROR);
                resultMsg.setData (storeEntry);
            }else if(operation == 0x0f){
                String describe = String.format("协调器%s重启成功",key);
                storeEntry.setDescription (describe);
                resultMsg.setData (storeEntry);
            }
        }
        logger.info("gateway message store[{}]",storeEntry.getDescription());
//        save (storeEntry);
        saveReal (storeEntry);
        nofity (resultMsg);
    }

    @Override
    protected NodeRequestMessage onCommand(TaskEntity taskEntity) {
        String id =taskEntity.getMethodName();
        if(StringUtils.isNullOrEmpty(id))
            return null;
        if(taskEntity.getMethodName ().equals ("down"))
            GatewayServer.getInstance ().stop ();
        if(CommandOperations.containsKey(id)) {
            Integer oper=CommandOperations.get(id);
            int length=4;
            return new NodeRequestMessage(getzhnyType() + 128, length, oper, Integer.parseInt(getConfig().getAddress()), null);
        }
        return null;
    }

    @Override
    protected int getzhnyType() {
        return 10;
    }

    @Override
    protected void disposable() {

    }
}
